home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Modules / timemodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-29  |  8.4 KB  |  389 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Time module */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29. #include "ceval.h"
  30.  
  31. #ifdef macintosh
  32. #include <time.h>
  33. #else
  34. #include <sys/types.h>
  35. #endif
  36.  
  37. #ifdef QUICKWIN
  38. #include <io.h>
  39. #endif
  40.  
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44.  
  45. #ifdef HAVE_SELECT
  46. #include "myselect.h"
  47. #else
  48. #include "mytime.h"
  49. #endif
  50.  
  51. #ifdef HAVE_FTIME
  52. #include <sys/timeb.h>
  53. #endif
  54.  
  55. #ifdef _M_IX86
  56. #include <windows.h>
  57. #define timezone _timezone
  58. #endif
  59.  
  60. /* Forward declarations */
  61. static int floatsleep PROTO((double));
  62. static double floattime PROTO(());
  63.  
  64. static object *
  65. time_time(self, args)
  66.     object *self;
  67.     object *args;
  68. {
  69.     double secs;
  70.     if (!getnoarg(args))
  71.         return NULL;
  72.     secs = floattime();
  73.     if (secs == 0.0) {
  74.         err_errno(IOError);
  75.         return NULL;
  76.     }
  77.     return newfloatobject(secs);
  78. }
  79.  
  80. #ifdef HAVE_CLOCK
  81.  
  82. #ifndef CLOCKS_PER_SEC
  83. #define CLOCKS_PER_SEC 1000000
  84. #endif
  85.  
  86. static object *
  87. time_clock(self, args)
  88.     object *self;
  89.     object *args;
  90. {
  91.     if (!getnoarg(args))
  92.         return NULL;
  93.     return newfloatobject(((double)clock()) / CLOCKS_PER_SEC);
  94. }
  95. #endif /* HAVE_CLOCK */
  96.  
  97. static object *
  98. time_sleep(self, args)
  99.     object *self;
  100.     object *args;
  101. {
  102.     double secs;
  103.     if (!getargs(args, "d", &secs))
  104.         return NULL;
  105.     BGN_SAVE
  106.     if (floatsleep(secs) != 0) {
  107.         RET_SAVE
  108.         return NULL;
  109.     }
  110.     END_SAVE
  111.     INCREF(None);
  112.     return None;
  113. }
  114.  
  115. static object *
  116. time_convert(when, function)
  117.     time_t when;
  118.     struct tm * (*function) PROTO((const time_t *));
  119. {
  120.     struct tm *p = function(&when);
  121.     return mkvalue("(iiiiiiiii)",
  122.                p->tm_year + 1900,
  123.                p->tm_mon + 1, /* Want January == 1 */
  124.                p->tm_mday,
  125.                p->tm_hour,
  126.                p->tm_min,
  127.                p->tm_sec,
  128.                (p->tm_wday + 6) % 7, /* Want Monday == 0 */
  129.                p->tm_yday + 1, /* Want January, 1 == 1 */
  130.                p->tm_isdst);
  131. }
  132.  
  133. static object *
  134. time_gmtime(self, args)
  135.     object *self;
  136.     object *args;
  137. {
  138.     double when;
  139.     if (!getargs(args, "d", &when))
  140.         return NULL;
  141.     return time_convert((time_t)when, gmtime);
  142. }
  143.  
  144. static object *
  145. time_localtime(self, args)
  146.     object *self;
  147.     object *args;
  148. {
  149.     double when;
  150.     if (!getargs(args, "d", &when))
  151.         return NULL;
  152.     return time_convert((time_t)when, localtime);
  153. }
  154.  
  155. static int
  156. gettmarg(args, p)
  157.     object *args;
  158.     struct tm *p;
  159. {
  160.     if (!getargs(args, "(iiiiiiiii)",
  161.              &p->tm_year,
  162.              &p->tm_mon,
  163.              &p->tm_mday,
  164.              &p->tm_hour,
  165.              &p->tm_min,
  166.              &p->tm_sec,
  167.              &p->tm_wday,
  168.              &p->tm_yday,
  169.              &p->tm_isdst))
  170.         return 0;
  171.     if (p->tm_year >= 1900)
  172.         p->tm_year -= 1900;
  173.     p->tm_mon--;
  174.     p->tm_wday = (p->tm_wday + 1) % 7;
  175.     p->tm_yday--;
  176.     return 1;
  177. }
  178.  
  179. static object *
  180. time_asctime(self, args)
  181.     object *self;
  182.     object *args;
  183. {
  184.     struct tm buf;
  185.     char *p;
  186.     if (!gettmarg(args, &buf))
  187.         return NULL;
  188.     p = asctime(&buf);
  189.     if (p[24] == '\n')
  190.         p[24] = '\0';
  191.     return newstringobject(p);
  192. }
  193.  
  194. static object *
  195. time_ctime(self, args)
  196.     object *self;
  197.     object *args;
  198. {
  199.     double dt;
  200.     time_t tt;
  201.     char *p;
  202.     if (!getargs(args, "d", &dt))
  203.         return NULL;
  204.     tt = dt;
  205.     p = ctime(&tt);
  206.     if (p[24] == '\n')
  207.         p[24] = '\0';
  208.     return newstringobject(p);
  209. }
  210.  
  211. static object *
  212. time_mktime(self, args)
  213.     object *self;
  214.     object *args;
  215. {
  216.     struct tm buf;
  217.     if (!gettmarg(args, &buf))
  218.         return NULL;
  219.     return newintobject((long)mktime(&buf));
  220. }
  221.  
  222. static struct methodlist time_methods[] = {
  223.     {"time",    time_time},
  224. #ifdef HAVE_CLOCK
  225.     {"clock",    time_clock},
  226. #endif
  227.     {"sleep",    time_sleep},
  228.     {"gmtime",    time_gmtime},
  229.     {"localtime",    time_localtime},
  230.     {"asctime",    time_asctime},
  231.     {"ctime",    time_ctime},
  232.     {"mktime",    time_mktime},
  233.     {NULL,        NULL}        /* sentinel */
  234. };
  235.  
  236. void
  237. inittime()
  238. {
  239.     object *m, *d;
  240.     m = initmodule("time", time_methods);
  241.     d = getmoduledict(m);
  242. #ifdef HAVE_TZNAME
  243.     tzset();
  244.     dictinsert(d, "timezone", newintobject((long)timezone));
  245. #ifdef HAVE_ALTZONE
  246.     dictinsert(d, "altzone", newintobject((long)altzone));
  247. #else
  248.     dictinsert(d, "altzone", newintobject((long)timezone-3600));
  249. #endif
  250.     dictinsert(d, "daylight", newintobject((long)daylight));
  251.     dictinsert(d, "tzname", mkvalue("(zz)", tzname[0], tzname[1]));
  252. #else /* !HAVE_TZNAME */
  253. #if HAVE_TM_ZONE
  254.     {
  255. #define YEAR ((time_t)((365 * 24 + 6) * 3600))
  256.         time_t t;
  257.         struct tm *p;
  258.         long winterzone, summerzone;
  259.         char wintername[10], summername[10];
  260.         /* XXX This won't work on the southern hemisphere.
  261.            XXX Anybody got a better idea? */
  262.         t = (time((time_t *)0) / YEAR) * YEAR;
  263.         p = localtime(&t);
  264.         winterzone = -p->tm_gmtoff;
  265.         strncpy(wintername, p->tm_zone ? p->tm_zone : "   ", 9);
  266.         wintername[9] = '\0';
  267.         t += YEAR/2;
  268.         p = localtime(&t);
  269.         summerzone = -p->tm_gmtoff;
  270.         strncpy(summername, p->tm_zone ? p->tm_zone : "   ", 9);
  271.         summername[9] = '\0';
  272.         dictinsert(d, "timezone", newintobject(winterzone));
  273.         dictinsert(d, "altzone", newintobject(summerzone));
  274.         dictinsert(d, "daylight",
  275.                newintobject((long)(winterzone != summerzone)));
  276.         dictinsert(d, "tzname",
  277.                mkvalue("(zz)", wintername, summername));
  278.     }
  279. #endif /* HAVE_TM_ZONE */
  280. #endif /* !HAVE_TZNAME */
  281. }
  282.  
  283.  
  284. /* Implement floattime() for various platforms */
  285.  
  286. static double
  287. floattime()
  288. {
  289.     /* There are three ways to get the time:
  290.        (1) gettimeofday() -- resolution in microseconds
  291.        (2) ftime() -- resolution in milliseconds
  292.        (3) time() -- resolution in seconds
  293.        In all cases the return value is a float in seconds.
  294.        Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
  295.        fail, so we fall back on ftime() or time().
  296.        Note: clock resolution does not imply clock accuracy! */
  297. #ifdef HAVE_GETTIMEOFDAY
  298.     {
  299.     struct timeval t;
  300.     if (gettimeofday(&t, (struct timezone *)NULL) == 0)
  301.         return (double)t.tv_sec + t.tv_usec*0.000001;
  302.     }
  303. #endif /* !HAVE_GETTIMEOFDAY */
  304.     {
  305. #ifdef HAVE_FTIME
  306.     struct timeb t;
  307.     ftime(&t);
  308.     return (double)t.time + (double)t.millitm * (double)0.001;
  309. #else /* !HAVE_FTIME */
  310.     time_t secs;
  311.     time(&secs);
  312.     return (double)secs;
  313. #endif /* !HAVE_FTIME */
  314.     }
  315. }
  316.  
  317.  
  318. /* Implement floatsleep() for various platforms.
  319.    When interrupted (or when another error occurs), return -1 and
  320.    set an exception; else return 0. */
  321.  
  322. static int
  323. floatsleep(secs)
  324.     double secs;
  325. {
  326. #ifdef HAVE_SELECT
  327.     struct timeval t;
  328.     double frac;
  329.     extern double fmod PROTO((double, double));
  330.     extern double floor PROTO((double));
  331.     frac = fmod(secs, 1.0);
  332.     secs = floor(secs);
  333.     t.tv_sec = (long)secs;
  334.     t.tv_usec = (long)(frac*1000000.0);
  335.     if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
  336.         err_errno(IOError);
  337.         return -1;
  338.     }
  339. #else /* !HAVE_SELECT */
  340. #ifdef macintosh
  341. #define MacTicks    (* (long *)0x16A)
  342.     long deadline;
  343.     deadline = MacTicks + (long)(secs * 60.0);
  344.     while (MacTicks < deadline) {
  345.         if (sigcheck())
  346.             return -1;
  347.     }
  348. #else /* !macintosh */
  349. #ifdef MSDOS
  350.     struct timeb t1, t2;
  351.     double frac;
  352.     extern double fmod PROTO((double, double));
  353.     extern double floor PROTO((double));
  354.     if (secs <= 0.0)
  355.         return;
  356.     frac = fmod(secs, 1.0);
  357.     secs = floor(secs);
  358.     ftime(&t1);
  359.     t2.time = t1.time + (int)secs;
  360.     t2.millitm = t1.millitm + (int)(frac*1000.0);
  361.     while (t2.millitm >= 1000) {
  362.         t2.time++;
  363.         t2.millitm -= 1000;
  364.     }
  365.     for (;;) {
  366. #ifdef QUICKWIN
  367.         _wyield();
  368. #endif
  369.         if (sigcheck())
  370.             return -1;
  371.         ftime(&t1);
  372.         if (t1.time > t2.time ||
  373.             t1.time == t2.time && t1.millitm >= t2.millitm)
  374.             break;
  375.     }
  376. #else /* !MSDOS */
  377. #ifdef _M_IX86
  378.     /* XXX Can't interrupt this sleep */
  379.     Sleep((int)(secs*1000));
  380. #else /* _M_IX86 */
  381.     /* XXX Can't interrupt this sleep */
  382.     sleep((int)secs);
  383. #endif /* _M_IX86 */
  384. #endif /* !MSDOS */
  385. #endif /* !macintosh */
  386. #endif /* !HAVE_SELECT */
  387.     return 0;
  388. }
  389.